package ro.noelx.connections;

import java.util.List;

import ro.noelx.android.tools.dao.Column;
import ro.noelx.android.tools.dao.Configuration;
import ro.noelx.android.tools.dao.Database;
import ro.noelx.android.tools.dao.Table;
import ro.noelx.android.tools.dao.UIConfiguration;
import ro.noelx.android.tools.ui.EntityListView;
import ro.noelx.connections.model.Person;
import ro.noelx.connections.model.Relation;
import ro.noelx.connections.model.Verb;
import ro.noelx.connections.ui.PersonListAdapter;
import ro.noelx.connections.ui.RelationListAdapter;
import ro.noelx.connections.ui.VerbListAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

public class Connections extends Activity {
	private ListView ls2;
	private TabHost myTabHost;
	private EntityListView currentEntityListView = null;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		final Configuration configuration = new Configuration(getDatabase(),
				this);
		UIConfiguration.getInstance().addEntityAdapter(Verb.class.getName(),
				new VerbListAdapter());
		UIConfiguration.getInstance().addEntityAdapter(Person.class.getName(),
				new PersonListAdapter());
		UIConfiguration.getInstance().addEntityAdapter(
				Relation.class.getName(), new RelationListAdapter());

		final EntityListView<Verb> verbListView = new EntityListView<Verb>(
				Connections.this, configuration.getEntityDAO(Verb.class
						.getName()), Verb.class.getName());
		final EntityListView<Person> personListView = new EntityListView<Person>(
				Connections.this, configuration.getEntityDAO(Person.class
						.getName()), Person.class.getName());
		final EntityListView<Relation> relationListView = new EntityListView<Relation>(
				Connections.this, configuration.getEntityDAO(Relation.class
						.getName()), Relation.class.getName());
		currentEntityListView = relationListView;
		ls2 = new ListView(Connections.this);

		setContentView(R.layout.main);
		this.myTabHost = (TabHost) this.findViewById(R.id.th_set_menu_tabhost);
		this.myTabHost.setup();

		TabSpec connectionsTab = myTabHost.newTabSpec("TAB_RELATIONS");

		connectionsTab.setContent(new TabHost.TabContentFactory() {
			public View createTabContent(String tag) {
				return relationListView.getListView();
			}
		});

		connectionsTab.setIndicator("Interactions", getResources().getDrawable(
				R.drawable.interaction));
		myTabHost.addTab(connectionsTab);

		// Persons tab
		TabSpec personsTab = myTabHost.newTabSpec("TAB_PERSONS");
		personsTab.setIndicator("Persons", getResources().getDrawable(
				R.drawable.persons));
		personsTab.setContent(new TabHost.TabContentFactory() {
			public View createTabContent(String tag) {
				return personListView.getListView();
			}
		});
		myTabHost.addTab(personsTab);

		// Verbs tab
		TabSpec verbsTab = myTabHost.newTabSpec("TAB_VERBS");
		verbsTab.setIndicator("Verbs", getResources().getDrawable(
				R.drawable.lightning));
		verbsTab.setContent(new TabHost.TabContentFactory() {
			public View createTabContent(String tag) {
				return verbListView.getListView();
			}
		});
		myTabHost.addTab(verbsTab);

		myTabHost.setOnTabChangedListener(new OnTabChangeListener() {

			public void onTabChanged(String tabId) {
				if (tabId.equals("TAB_VERBS")) {
					currentEntityListView = verbListView;
				} else if (tabId.equals("TAB_PERSONS")) {
					currentEntityListView = personListView;
				} else if (tabId.equals("TAB_RELATIONS")) {
					currentEntityListView = relationListView;
				} else {
					currentEntityListView = null;
				}
				if (currentEntityListView != null) {
					currentEntityListView.refresh();
				}
				System.out.println("tab changed:" + tabId);
			}
		});
	}

	private Database getDatabase() {
		Database database = new Database("connections", 5);
		Table tablePerson = new Table("person", Person.class.getName());
		tablePerson.addColumn(new Column("_id", Column.INTEGER, false, true,
				true));
		tablePerson.addColumn(new Column("name", Column.TEXT, true, false,
				false));
		database.addTable(tablePerson);

		Table tableVerb = new Table("verb", Verb.class.getName());
		tableVerb
				.addColumn(new Column("_id", Column.INTEGER, false, true, true));
		tableVerb
				.addColumn(new Column("value", Column.TEXT, true, false, false));
		database.addTable(tableVerb);

		Table tableRelation = new Table("relation", Relation.class.getName());
		tableRelation.addColumn(new Column("_id", Column.INTEGER, false, true,
				true));
		tableRelation.addColumn(new Column("person1", Column.INTEGER, true,
				false, false, true, Person.class.getName()));
		tableRelation.addColumn(new Column("person2", Column.INTEGER, true,
				false, false, true, Person.class.getName()));
		tableRelation.addColumn(new Column("verb", Column.INTEGER, true, false,
				false, true, Verb.class.getName()));
		database.addTable(tableRelation);

		return database;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		System.out.println("onCreateOptionsMenu");

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		System.out.println("onPrepareOptionsMenu");
		menu.clear();
		if (currentEntityListView != null) {
			List<String> menuItemSs = currentEntityListView.getMenuItems();
			for (String menuItemS : menuItemSs) {
				menu.add(menuItemS);
			}
		}
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		System.out.println("onOptionsItemSelected:" + item.getTitle());
		if (currentEntityListView != null) {
			currentEntityListView.menuItemSelected((String) item.getTitle());
		}
		return super.onOptionsItemSelected(item);
	}
}